package com.zang.stackqueue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;

/**
 * @author ZVerify
 * @since 2022/10/20 20:48
 **/
public class 前K个高频元素 {
    public static void main(String[] args) {
        前K个高频元素 a = new 前K个高频元素();
        int[] ints = a.topKFrequent(new int[]{1, 2}, 2);
        System.out.println();
    }
    public int[] topKFrequent(int[] nums, int k) {

        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int num : nums) {
            if (!hashMap.containsKey(num)){
                hashMap.put(num,1);
            }else {
                hashMap.put(num,hashMap.get(num)+1);
            }
        }

        HashMap<Integer, Integer> map = new HashMap<>();
        int index = 0;
        for (Integer integer : hashMap.keySet()) {
            if (!map.containsValue(hashMap.get(integer))){
                map.put(hashMap.get(integer)+1,integer);
            }
            map.put(hashMap.get(integer),integer);
        }
        Integer[] ints = new Integer[map.size()];
        for (Integer integer : map.keySet()) {
            if (!hashMap.containsKey(integer)){
                integer =  integer-1;
            }
            ints[index++] = integer;
        }
        index = 0;
        Arrays.sort(ints,new Comparator<Integer>(){

            @Override
            public int compare(Integer o1, Integer o2) {
                return o2=o1;
            }
        });
        int[] ints1 = new int[k];
        while (k>0){
            ints1[index] =map.get(ints[index++]);
            k--;
        }
        return ints1;

    }
}
